package com.winway.sixbook.ui;

import android.app.Activity; 
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;  
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import com.winway.sixbook.bean.SeedData;
import com.winway.sixbook.custom.CloseActivityClass;
import com.winway.sixbook.service.SeedManager;

import com.winway.sixbook.HomeActivity;
import com.winway.sixbook.R;  
public class SeedAddActivity extends Activity {
	private static final String TAG = "SeedAddActivity";
	private Button seedAddButton = null; 
	private EditText seedNameInput;
	private SeedManager mseedManager;
	
	private ImageButton top_tbn_fanhui;
	private Button top_tbn_xinzeng;
	private TextView title;
	private TextView seedId;
	String typename = "";
	String typeid = "0";
	private Button delbutton = null;
	AlertDialog builder = null;
	private boolean flagnew = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.seedaddactivity);
		CloseActivityClass.activityList.add(this);
		
		if (mseedManager == null) {
			mseedManager = new SeedManager(this);
			mseedManager.openDataBase();
        }
		mainInit();
	}

	private void mainInit(){
		seedNameInput = (EditText) findViewById(R.id.seedNameInput);
		seedAddButton = (Button)findViewById(R.id.seedAddButton);  
		top_tbn_fanhui = (ImageButton)findViewById(R.id.nagivate_btn_left);
        top_tbn_xinzeng = (Button)findViewById(R.id.navigate_btn_right);
        title = (TextView)findViewById(R.id.title);
        seedId = (TextView)findViewById(R.id.seedId);
        delbutton = (Button)findViewById(R.id.delseedbutton);  
		
        
        top_tbn_xinzeng.setVisibility(View.INVISIBLE);
        title.setText("种子类型设置");
        
        Bundle bundle = new Bundle();
        bundle = this.getIntent().getExtras();
        Log.v(TAG, ""+bundle);
        typename = bundle.getString("typename");
        typeid = bundle.getString("typeid");
        seedId.setText(typeid);
        seedNameInput.setText(typename);
        
        top_tbn_fanhui.setOnClickListener(mListener);
        seedAddButton.setOnClickListener(mListener); 
        if(null != typeid && !"".equals(typeid)){
        	delbutton.setVisibility(View.VISIBLE); 
        }
        delbutton.setOnClickListener(mListener); 
	}
	
	OnClickListener mListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.seedAddButton:
					String seedName = seedNameInput.getText().toString().trim();
					if("".equals(seedName)){
						Toast.makeText(SeedAddActivity.this, getString(R.string.type_empty), Toast.LENGTH_SHORT).show();
					}else{
						if(null != typeid && !"".equals(typeid)){
							if(Integer.parseInt(typeid)>0)
							seedupdate(typeid,seedName,typename);
						}else{
							seedAdd(seedName);
						}
					}
					break; 
				case R.id.delseedbutton:
					seeddel(typeid);
					break;
			}
		}
	};
	
	private void seedAdd(String seedName)
	{
		mseedManager.openDataBase(); 
		//判断数据库是否存在
		int count=mseedManager.findSeedByName(seedName);
		if(count>0){
			Toast.makeText(this, getString(R.string.name_already_exist, seedName),Toast.LENGTH_SHORT).show();
			return ;
		}
		
		SeedData mUser = new SeedData();
		mUser.setSeedName(seedName);
		mseedManager.openDataBase();
		long flag = mseedManager.insertSeedData(mUser);
		if (flag == -1) {
			Toast.makeText(this, getString(R.string.register_fail),Toast.LENGTH_SHORT).show();
		}else{
			Toast.makeText(this, getString(R.string.SeedAdd_Sucess),Toast.LENGTH_SHORT).show();
			Intent intentset = new Intent(SeedAddActivity.this, SeedChooseActivity.class);
			startActivity(intentset);
			finish();
		}
	}
	
	protected void seedupdate(String typeid2,String seedName, String oldname) {
		mseedManager.openDataBase();
		if(!seedName.trim().equals(oldname.trim())){
			//判断数据库是否存在
			int count=mseedManager.findSeedByName(seedName);
			if(count>0){
				Toast.makeText(this, getString(R.string.name_already_exist, seedName),Toast.LENGTH_SHORT).show();
				return ;
			}
		}
		mseedManager.updateSeedData(typeid2, seedName);
		Intent intentset = new Intent(SeedAddActivity.this, SeedChooseActivity.class);
		startActivity(intentset);
	}
	
	private void seeddel(final String id){
		builder = new AlertDialog.Builder(SeedAddActivity.this)
		.setIcon(R.drawable.ic_launcher)
		.setTitle("温馨提示：")
		.setMessage("您是否要删除该数据吗")
		.setPositiveButton("确定",new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int whichButton) {
				mseedManager.openDataBase();
				flagnew = mseedManager.deleteSeedData(Integer.parseInt(id));
				if(null != id && !"".equals(id)){
					if (!flagnew) {
						Toast.makeText(SeedAddActivity.this, getString(R.string.del_fail),Toast.LENGTH_SHORT).show();
					}else{
						Toast.makeText(SeedAddActivity.this, getString(R.string.del_sucess),Toast.LENGTH_SHORT).show();
					}
				}else{
					Toast.makeText(SeedAddActivity.this, getString(R.string.del_fail),Toast.LENGTH_SHORT).show();
				}
				Intent counterset = new Intent(SeedAddActivity.this,SeedChooseActivity.class);
				startActivity(counterset);
			}
		}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog,
					int whichButton) {
				builder.dismiss();
			}
		}).show();
		
		
	}

	@Override
	protected void onResume() {
		if (mseedManager == null) {
			mseedManager = new SeedManager(this);
			mseedManager.openDataBase();
        }
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		if (mseedManager != null) {
			mseedManager.closeDataBase();
			mseedManager = null;
        }
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		if (mseedManager != null) {
			mseedManager.closeDataBase();
			mseedManager = null;
        }
		super.onPause();
	}
	
	/**
	 * 捕捉back
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Intent counter1 = new Intent(SeedAddActivity.this, SeedChooseActivity.class);
			startActivity(counter1);
			finish();
			
			return true;
		}
		
		return super.onKeyDown(keyCode, event);
	}
}
